/**
 * Created by jianhuang on 2015/1/9.
 */
define(['jquery'],function($){
    var utils={
        // of the passed-in callback, to be repeatedly applied in other Underscore
        createCallback:function (func, context, argCount) {
            if (context === void 0) return func;
            switch (argCount == null ? 3 : argCount) {
                case 1: return function(value) {
                    return func.call(context, value);
                };
                case 2: return function(value, other) {
                    return func.call(context, value, other);
                };
                case 3: return function(value, index, collection) {
                    return func.call(context, value, index, collection);
                };
                case 4: return function(accumulator, value, index, collection) {
                    return func.call(context, accumulator, value, index, collection);
                };
            }
            return function() {
                return func.apply(context, arguments);
            };
        },

        operDataWithFunc:function(name,callBack,context){
            var callName=utils._createCallback(callBack,context);
            return callName(name,context);
        },

        send:function(type,url,data,successs,error,before){
            $.ajax({
                type:type,
                url:url,
                data:data,
                dataType:"json",
                success:successs,
                error:error||function(){console.log("页面请求失败");},
                beforeSend:before||function(){console.log("页面请求中...");}
            });
        },

        getQueryString:function(name){
            var params = location.search.substring(1).toLowerCase();
            var paramList = [];
            var param = null;
            var parami;
            if (params.length > 0) {
                if (params.indexOf("&") >= 0) {
                    paramList = params.split("&");
                } else {
                    paramList[0] = params;
                }
                for (var i = 0, listLength = paramList.length; i < listLength; i++) {
                    parami = paramList[i].indexOf(name + "=");
                    if (parami >= 0) {
                        param = paramList[i].substr(parami + (name + "=").length);
                    }
                }
            }
            return param;
        },

        /*随机数*/
        getRandomNum: function(min,max){
            var range = max - min;
            var rand = Math.random();
            return(min + Math.round(rand * range));
        }
    };

    return utils;
});